/*
 * @Author: ✨Fireflx 
 * @Date: 2022-02-15 09:34:12 
 * @Last Modified by: ✨Fireflx
 * @Last Modified time: 2022-03-03 11:17:56
 */

const jwt = require('jsonwebtoken')
const { User } = require("../models")
const Key = require('../key')

const key = async ctx => {
    const publicKey = await Key.getKey();
    ctx.body = {publicKey}
}

const userLogin = async (ctx) => {//🌟
    let { username, pwd } = ctx.request.body;
    pwd = await Key.decrypt(pwd)//🌟
    // await findOne(User,ctx,{username,pwd})
    await User.findOne({ username, pwd })
        .then(res => {
            if (res) {
                const token = jwt.sign({//🌟
                    username: res.username,
                    _id: res._id
                }, 'jwt-server-admin', {//🌟//🌟
                    //token时长为1周 s
                    expiresIn: 3600 * 24 * 7
                })
                ctx.body = {
                    code: 200,
                    msg: '登录成功！',
                    token,
                    data:res
                }
            } else {
                ctx.body = {
                    code: 300,
                    msg: "账户或密码错误！",
                }
            }
        }).catch(err => {
            ctx.body = {
                code: 500,
                msg: '登录异常！',
                err
            }
        })
}

const userReg = async ctx => {
    const { username, pwd } = ctx.request.body
    let hasCurrentUser = false
    await User.findOne({ username })
        .then(res => {
            if(res) hasCurrentUser = true
        }).catch(err => {
            hasCurrentUser = true
        })
    if(hasCurrentUser){
        ctx.body = {
            code: 500,
            msg: '账号已存在！',
        }
        return;
    };
    await User.create({ username, pwd })
        .then(res => {
            ctx.body = res ? {
                code: 200,
                msg: "注册成功！",
            } : {
                code: 300,
                msg: '注册失败！'
            }
        }).catch(err => {
            ctx.body = {
                code: 500,
                msg: '注册异常！'
            }
        })
}

const userVerify = async ctx => {//🌟
    const token = ctx.header.authorization.replace('Bearer ','')
    try{
        const result = jwt.verify(token, 'jwt-server-admin')//🌟
        await User.findOne({_id:result._id})
            .then(res=>{
                ctx.body = res ? {
                    code: 200,
                    msg: "登录验证成功！",
                } : {
                    code: 300,
                    msg: '登录验证失败！'
                } 
            }).catch(err=>{
                ctx.body = {
                    code: 500,
                    msg: '登录验证失败！',
                }
            })
    }catch{
        ctx.body = {
            code: 500,
            msg: '登录验证失败！'
        }
    }
}

const updatePwd = async ctx => {
    const {_id,pwd} = ctx.request.body
    await User.updateOne({_id},{pwd})
        .then(res=>{
            ctx.body = res.modifiedCount ? {
                code: 200,
                msg: "修改密码成功！",
                data:res
            } : {
                code: 300,
                msg: '修改密码重复！'
            } 
        }).catch(err=>{
            ctx.body = {
                code: 500,
                msg: '修改密码失败！',
            }
        })
}

const updatePersonal = async ctx => {
    const {
        _id,
        avatar = '',
        sex = '',
        desc = '',
        phone = '',
        email = ''
    } = ctx.request.body;
    await User.updateOne({_id},{
        avatar,
        sex,
        desc,
        phone,
        email
    }).then(res=>{
        ctx.body = res.modifiedCount ? {
            code: 200,
            msg: "修改资料成功！",
        } : {
            code: 300,
            msg: '已更改零项资料！'
        }
    }).catch(err=>{
        ctx.body = {
            code: 500,
            msg: '修改资料失败！',
        }
    })
}

module.exports = {
    key,
    userLogin,
    userReg,
    userVerify,
    updatePwd,
    updatePersonal
}